{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a0001786",
   "metadata": {},
   "source": [
    "* 时间：2022-11-1/week11\n",
    "* 记录人：李奥琪\n",
    "----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6a345e3",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1. 字典的方法\n",
    "> 2. 实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "068437a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Mary\",\n",
    "    1800003:\"Alex\",\n",
    "    1800004:\"小明\",\n",
    "}\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "13f5fa4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: 'Jack', 1800002: 'Mary', 1800003: 'Alex', 1800004: '小明'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(user_list,type(user_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c10e1fc8",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_20948/1729018842.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0muser_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m#证明字典是无序的，没办法用索引取值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "user_list[0] #证明字典是无序的，没办法用索引取值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b28d5baa",
   "metadata": {},
   "source": [
    "# 1.字典的方法\n",
    "\n",
    "* len()查看长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "76a3e340",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(user_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "520303c4",
   "metadata": {},
   "source": [
    "* in 查询成员是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "db3aaf6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1800001 in user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a45cb52e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list[1800001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6bc5ab5",
   "metadata": {},
   "source": [
    "# 1.2 字典的一些方法\n",
    "> 1. 增删改查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5732fa36",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Mary\",\n",
    "    1800003:\"Alex\",\n",
    "    1800004:\"小明\",\n",
    "}\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8a17d47",
   "metadata": {},
   "source": [
    "* 字典增加\n",
    "> 1. 先查询要增加的key是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "32058abd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jan', 1800002: 'Mary', 1800003: 'Alex', 1800004: '小明'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "51b11bc7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "增加成功\n"
     ]
    }
   ],
   "source": [
    "if 1800005 in user_list:\n",
    "    print(\"该用户已存在，请更换key\")\n",
    "else:\n",
    "    user_list[1800005] = \"李华\"\n",
    "    print(\"增加成功\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed7f0ccb",
   "metadata": {},
   "source": [
    "* 字典的修改\n",
    "> 1. 先查询要修改的key是否存在\n",
    "> 2. 如果不存在key,可能需要考虑是否新增用户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "92db343e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信息更新成功\n"
     ]
    }
   ],
   "source": [
    "if 1800001 in user_list:\n",
    "    user_list[1800001]=\"Jan\"\n",
    "    print(\"信息更新成功\")\n",
    "else:\n",
    "    print(\"该key找不到\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4dfd7df8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jan',\n",
       " 1800002: 'Mary',\n",
       " 1800003: 'Alex',\n",
       " 1800004: '小明',\n",
       " 1800005: '李华'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "248fa574",
   "metadata": {},
   "outputs": [],
   "source": [
    "* 字典的查询：get()可以查询到该key的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1d828dae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(user_list.get(1800009))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd9bdeac",
   "metadata": {},
   "source": [
    "# 1.2 字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "017bc822",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1800001, 'Jan'), (1800002, 'Mary'), (1800003, 'Alex'), (1800004, '小明'), (1800005, '李华')])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2df9b619",
   "metadata": {},
   "source": [
    "* 字典的循环遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b325aee5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回顾\n",
    "strings = \"hello world\"\n",
    "lsits = [18,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e2fb37d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      " \n",
      "w\n",
      "o\n",
      "r\n",
      "l\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "for i in strings:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8bfc008a",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Mary\",\n",
    "    1800003:\"Alex\",\n",
    "    1800004:\"小明\",\n",
    "}\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "52af7293",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1800001, 'Jack'), (1800002, 'Mary'), (1800003, 'Alex'), (1800004, '小明')])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "db4b5d82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1800001 Jack\n",
      "1800002 Mary\n",
      "1800003 Alex\n",
      "1800004 小明\n"
     ]
    }
   ],
   "source": [
    "#字典的循环\n",
    "for k,v in user_list.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "de06492d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys([1800001, 1800002, 1800003, 1800004])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dict.key() : 取出全部key\n",
    "user_list.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2cf432c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['Jack', 'Mary', 'Alex', '小明'])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "729dd54b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800004, '小明')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pop()\n",
    "user_list.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "c00378a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.pop(1800001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ae54972e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800002: 'Mary', 1800003: 'Alex'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "bafba2e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Mary': 100, 'Alex': 100}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.values()\n",
    "re_game = {}\n",
    "re_game.fromkeys(user_list.values(),100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "62fd8a0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "books = {'runoob':'菜鸟教程','google':'Google 搜索'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f05812e1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
